package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.StudyPlan;
import com.battle.pojo.entity.StudyPlanUser;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.studyPlan.*;
import com.battle.pojo.vo.studyPlan.*;
import com.battle.pojo.dto.studyPlan.*;
import com.battle.service.repository.in.StudyPlanRepository;
import com.battle.service.repository.in.StudyPlanUserRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.StudyPlanService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：学习计划服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class StudyPlanServiceImpl implements StudyPlanService {

    private final StudyPlanRepository studyPlanRepository;
    private final StudyPlanUserRepository studyPlanUserRepository;
    private final UserRepository userRepository;

    public StudyPlanServiceImpl(StudyPlanRepository studyPlanRepository, StudyPlanUserRepository studyPlanUserRepository, UserRepository userRepository) {
        this.studyPlanRepository = studyPlanRepository;
        this.studyPlanUserRepository = studyPlanUserRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建学习计划
     *
     * @param param
     */
    @Override
    public void createStudyPlan(CreateStudyPlanParam param) {
        StudyPlan studyPlan = new StudyPlan();
        BeanUtils.copyProperties(param, studyPlan);
        Long id = IdGenerator.SNOW.generate();
        studyPlan.setId(id);
        List<StudyPlan> studyPlanList = studyPlanRepository.getStudyPlanList(param.getEndTime(), param.getType(),null);
        if (CollectionUtil.isNotEmpty(studyPlanList)) throw new BizException("相同时间内只允许有一个学习计划");
        studyPlanRepository.create(studyPlan);
    }

    /**
     * 修改学习计划
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editStudyPlan(EditStudyPlanParam param) {
        Long id = param.getId();
        checkStudyPlanUser(id);
        StudyPlan studyPlan = studyPlanRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, studyPlan);
        List<StudyPlan> studyPlanList = studyPlanRepository.getStudyPlanList(param.getEndTime(), param.getType(),id);
        if (CollectionUtil.isNotEmpty(studyPlanList)) throw new BizException("相同时间内只允许有一个学习计划");
        studyPlanRepository.edit(studyPlan);
    }

    /**
     * 检查是否有用户已经学习了
     * @param planId
     */
    void checkStudyPlanUser(Long planId){
        StudyPlanUser user=new StudyPlanUser();
        user.setPlanId(planId);
        user.setIsDisable(false);
        user.setIsDelete(false);
        List<StudyPlanUser> listByEntity = studyPlanUserRepository.getListByEntity(user);
        if (CollectionUtil.isNotEmpty(listByEntity)) throw new BizException("用户已经开始学习，不可更改学习计划");
    }

    /**
     * 移除学习计划
     *
     * @param id
     */
    @Override
    public void removeStudyPlan(Long id) {
        checkStudyPlanUser(id);
        studyPlanRepository.removeById(id);
    }

    /**
     * 启动/禁用学习计划
     *
     * @param id
     */
    @Override
    public void enabledStudyPlan(Long id) {
        StudyPlan studyPlan = studyPlanRepository.getByIdCheck(id);
        studyPlan.setIsDisable(!studyPlan.getIsDisable());
        studyPlanRepository.edit(studyPlan);
    }

    /**
     * 获取学习计划详情
     *
     * @param id
     * @return
     */
    @Override
    public GetStudyPlanVo getStudyPlan(Long id) {
        StudyPlan studyPlan = studyPlanRepository.getByIdCheck(id);
        GetStudyPlanVo vo = new GetStudyPlanVo();
        BeanUtils.copyProperties(studyPlan, vo);
        return vo;
    }

    /**
     * 获取学习计划 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetStudyPlanListVo> getStudyPlanList(GetStudyPlanListParam param) {

        SelectStudyPlanDto dto = new SelectStudyPlanDto();
        BeanUtils.copyProperties(param, dto);
        Page<StudyPlan> page = studyPlanRepository.selectStudyPlan(dto);
        List<StudyPlan> studyPlanList = page.getRecords();
        List<GetStudyPlanListVo> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(studyPlanList)){
            //创建人
            List<Long> userIds = new ArrayList<>();
            studyPlanList.forEach(v ->{
                userIds.add(v.getCreator());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);

            for (StudyPlan studyPlan : studyPlanList) {
                GetStudyPlanListVo vo = new GetStudyPlanListVo();
                BeanUtils.copyProperties(studyPlan, vo);

                User create = userMap.get(studyPlan.getCreator());
                if (create != null) {
                    vo.setCreateName(create.getName());
                }
                vo.setCreateTime(studyPlan.getCreateTime());
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取学习计划下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetStudyPlanDownListVo> getStudyPlanDownList(GetStudyPlanDownListParam param) {
        StudyPlan po = new StudyPlan();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<StudyPlan> studyPlanList = studyPlanRepository.getListByEntity(po);

        List<GetStudyPlanDownListVo> list = new ArrayList<>();
        for (StudyPlan studyPlan : studyPlanList) {
            GetStudyPlanDownListVo vo = new GetStudyPlanDownListVo();
            BeanUtils.copyProperties(studyPlan, vo);
            list.add(vo);
        }
        return list;
    }


}



